package com.szh.lenovo0518;

import android.content.ContentProvider;
import android.content.ContentValues;
import android.content.UriMatcher;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.net.Uri;
import android.util.Log;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import java.io.BufferedReader;

public class MyProvider extends ContentProvider {
    private static final String TAG = "MyProvider";
    //UriMatcher对象
    static UriMatcher matcher = new UriMatcher(UriMatcher.NO_MATCH);//如果匹配不成功返回-1
    public static final int SELECT_BOOK_SUCCESS=100;
    public static final int SELECT_USER_SUCCESS=101;
    public static final int INSERT_BOOK_SUCCESS=200;
    public static final int INSERT_USER_SUCCESS=201;
    public static final int UPDATE_BOOK_SUCCESS=300;
    public static final int UPDATE_USER_SUCCESS=301;
    public static final int DELETE_BOOK_SUCCESS=400;
    public static final int DELETE_USER_SUCCESS=401;
    static {//静态代码块 1:Uri中资源的存放位置   2：Uri中的资源路径  3：匹配成功后返回的匹配码
        matcher.addURI("com.szh.lenovo0518","select_book",SELECT_BOOK_SUCCESS);
        matcher.addURI("com.szh.lenovo0518","select_user",SELECT_USER_SUCCESS);
        matcher.addURI("com.szh.lenovo0518","insert_book",INSERT_BOOK_SUCCESS);
        matcher.addURI("com.szh.lenovo0518","insert_user",INSERT_USER_SUCCESS);
        matcher.addURI("com.szh.lenovo0518","update_book",UPDATE_BOOK_SUCCESS);
        matcher.addURI("com.szh.lenovo0518","update_user",UPDATE_USER_SUCCESS);
        matcher.addURI("com.szh.lenovo0518","delete_book",DELETE_BOOK_SUCCESS);
        matcher.addURI("com.szh.lenovo0518","delete_user",DELETE_USER_SUCCESS);
    }
    MyOpenHelper helper;

    @Override
    public boolean onCreate() {//只执行一次 创建时
        //在此方法做初始化操作
        helper=new MyOpenHelper(getContext());
        return false;
    }

    @Nullable
    @Override
    public Cursor query(@NonNull Uri uri, @Nullable String[] projection, @Nullable String selection, @Nullable String[] selectionArgs, @Nullable String sortOrder) {
        SQLiteDatabase db=helper.getReadableDatabase();
        int code = matcher.match(uri);
        Log.d(TAG, "code: "+code+"");
        Cursor cursor=null;
        if (code == SELECT_BOOK_SUCCESS) {
            cursor = db.query("book", projection, selection, selectionArgs, null, null, sortOrder);
        } else if (code == SELECT_USER_SUCCESS) {
         cursor = db.query("user", projection, selection, selectionArgs, null, null, sortOrder);
        } else if (code == -1) {
            throw new IllegalArgumentException("query访问路径有误，请查询");
        }
        return cursor;
    }

    @Nullable
    @Override
    public Uri insert(@NonNull Uri uri, @Nullable ContentValues values) {
        int code = matcher.match(uri);
        if (code==INSERT_BOOK_SUCCESS) {
            //新增book表数据  新增成功 返回id 失败返回-1
            SQLiteDatabase db = helper.getWritableDatabase();
            long book = db.insert("book", null, values);
            if (book!=-1) {
                Uri uriInsert=Uri.parse("insert book success----"+book);
                return  uriInsert;
            }else {
                throw new IllegalArgumentException("insert访问路径有误，请查询");
            }

        } else if (code==INSERT_USER_SUCCESS) {
            //新增user表数据  新增成功 返回id 失败返回-1
            SQLiteDatabase db = helper.getWritableDatabase();
            long user = db.insert("user", null, values);
            if (user!=-1) {
                Uri uriInsert=Uri.parse("insert user success----"+user);
                return  uriInsert;
            }else {
                throw new IllegalArgumentException("insert访问路径有误，请查询");
            }
        }else {
            //路径错误
        }
        return null;
    }

    @Override
    public int delete(@NonNull Uri uri, @Nullable String selection, @Nullable String[] selectionArgs) {
        int code = matcher.match(uri);
        SQLiteDatabase db = helper.getWritableDatabase();
        int delete=0;
        switch (code){
            case DELETE_BOOK_SUCCESS:
               delete = db.delete("book", selection, selectionArgs);
                Log.d(TAG, "delete: "+delete);
                if (delete<=0) {
                    //删除失败
                    throw new IllegalArgumentException("delete语句有问题，请检查");
                }
                break;
            case DELETE_USER_SUCCESS:
                delete = db.delete("user", selection, selectionArgs);
                Log.d(TAG, "delete: "+delete);
                if (delete<=0) {
                    //删除失败
                    throw new IllegalArgumentException("delete语句有问题，请检查");
                }
                break;
            default: throw new IllegalArgumentException("路径有问题，请检查");
        }
        return 0;
    }

    @Override
    public int update(@NonNull Uri uri, @Nullable ContentValues values, @Nullable String selection, @Nullable String[] selectionArgs) {
        int code = matcher.match(uri);
        SQLiteDatabase db = helper.getWritableDatabase();
        int result=0;
        switch (code){
            case UPDATE_BOOK_SUCCESS:
                //返回值：影响的函数
                result = db.update("book", values, selection, selectionArgs);
                Log.d(TAG, "result: "+result);
                if (result <= 0) {
                    //修改失败
                    throw new IllegalArgumentException("update语句有问题，请检查");
                }
                break;
            case UPDATE_USER_SUCCESS:
                //返回值：影响的函数
                result = db.update("user", values, selection, selectionArgs);
                Log.d(TAG, "result: "+result);
                if (result <= 0) {
                    //修改失败
                    throw new IllegalArgumentException("update语句有问题，请检查");
                }
                break;
            default: throw new IllegalArgumentException("路径有问题，请检查");
        }
        return result;
    }

    @Nullable
    @Override
    public String getType(@NonNull Uri uri) {
        return null;
    }
}
